package com.stms.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.stms.bean.User;
import com.stms.dao.UserDao;

/**
 * 登录操作Servlet
 */
@WebServlet("/doLogin")
public class LoginServlet extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		HttpSession session = req.getSession();
		
		String uName = req.getParameter("username");
		String pWord = req.getParameter("password");
		
		UserDao userDao = new UserDao();
		User user = userDao.selectUserByUserNameAndPassword(uName,pWord);
		if(user!=null){
			session.setAttribute("isLogin", true);
			session.setAttribute("loginUser", user);
			resp.sendRedirect("index.jsp");
		}else{
			session.setAttribute("isLogin", false);
			session.setAttribute("msg", "用户名或密码错误");
			req.setAttribute("error", "用户名或密码错误，请重新输入。");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}

	}

}
